/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package invsys.Controladores;

import invsys.Modelo.Credito;
import invsys.Utilidades.ManejadorBaseDatos;
import invsys.Utilidades.ValidarorVistas;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.LinkedList;
import java.util.List;
/**
 *
 * @author dell
 */
public class ControlCredito {
           public static Credito load(ResultSet rs) throws SQLException{
        Credito c=new Credito();
        c.setId(rs.getString(1));
        c.setFecha(rs.getString(2));
        c.setSaldo(rs.getDouble(3));
        c.setClente(ControlCliente.BuscarClienteid(rs.getString(4)));
        return c;
    }  
  static ValidarorVistas vistas=new ValidarorVistas();
  private static List creditos;
  public static Connection con;
  public static void RegistrarCredito(Credito c) throws SQLException, IOException, Exception {
        ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
        mbd.conectar();
        con = mbd.getConexion();
        if (con == null) {
            throw new SQLException(" no hay conexion ");
        }
        PreparedStatement pst = null;

        try {

            pst = con.prepareStatement("Insert Into creditos values(?,?,?,?)");
            pst.setString(1, c.getId());
            pst.setString(2, c.getFecha());
            pst.setDouble(3, c.getSaldo());
            pst.setString(4, c.getClente().getId());
            pst.executeUpdate();      
           
        } finally {
            if (pst != null) {
                pst.close();
            }
        }
    }
  public static Credito BuscarCredito(String num) throws SQLException{
            if (num == null) {
          throw new SQLException("No hay elemento clave de la clase usuario");
        }
         ResultSet rs = null;
         PreparedStatement pst = null;
         Credito credito= null;
     try {
         ManejadorBaseDatos mbd=ManejadorBaseDatos.getInstancia();
         mbd.conectar();
         con=mbd.getConexion();
         pst = con.prepareStatement("select * from creditos where id= ?");
         pst.setString(1, num.trim());

         rs = pst.executeQuery();
         while(rs.next()) {
             credito = load(rs);
         }
    } finally {
         if (rs != null) {
            rs.close();
         }
         if (pst != null) {
           pst.close();
         }
          return credito;
    }
    }
  public static Credito BuscarCreditofecha(String fecha) throws SQLException{
            if (fecha == null) {
          throw new SQLException("No hay elemento clave de la clase usuarios");
        }
         ResultSet rs = null;
         PreparedStatement pst = null;
         Credito credito = null;
     try {
         ManejadorBaseDatos mbd=ManejadorBaseDatos.getInstancia();
         mbd.conectar();
         con=mbd.getConexion();
         pst = con.prepareStatement("select * from creditos where fecha= ?");
         pst.setString(1, fecha.trim());

         rs = pst.executeQuery();
         while(rs.next()) {
             credito = load(rs);
         }
    } finally {
         if (rs != null) {
            rs.close();
         }
         if (pst != null) {
           pst.close();
         }
          return credito;
    }
    }
  public static void ModificarCredito(Credito c, String ide) throws SQLException, Exception {
        if (BuscarCredito(ide) == null) {
            throw new SQLException("usuario no registrado ");
        }
        PreparedStatement pst = null;

        try {
            ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
            mbd.conectar();
            con = mbd.getConexion();
            pst = con.prepareStatement("UPDATE creditos SET id = ? ,"
                    + " fecha = ?,saldo = ?, cliente = ? WHERE id = ?");	
            pst.setString(1, c.getId());
            pst.setString(2, c.getFecha());
            pst.setDouble(3, c.getSaldo());
            pst.setString(4, c.getClente().getId());
            pst.setString(5, ide);
            pst.executeUpdate();


        } finally {
            if (pst != null) {
                pst.close();
            }
        }

    }
  public static void listarCredito() throws SQLException, Exception {
     ResultSet rs = null;
     PreparedStatement pst = null;
     creditos = new LinkedList();
     try {
         ManejadorBaseDatos mbd=ManejadorBaseDatos.getInstancia();
         mbd.conectar();
         con=mbd.getConexion();
        pst = con.prepareStatement("select * from creditos ");
        rs = pst.executeQuery();
        while(rs.next()) {
           creditos.add(load(rs));
        }
    } finally {
        if (rs != null) {
           rs.close();
       }
        if (pst != null) {
          pst.close();
       }
}
}
  public static boolean ExisteCredito(int ide){
        boolean existe=false;

        return existe;
    }
  public static void EliminarCredito(String ide) throws SQLException, Exception {
        if (BuscarCredito(ide) == null) {
            throw new SQLException("No hay elemento clave de la clase Usuario");
        }
        PreparedStatement pst = null;

        try {
            ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
            mbd.conectar();
            con = mbd.getConexion();
            pst = con.prepareStatement("delete from creditos where id= ?");
            pst.setString(1, ide.trim());
            pst.executeUpdate();

        } finally {
            if (pst != null) {
                pst.close();
            }
        }

    }
  public static void listarCredito1(String sql) throws SQLException, Exception {
     ResultSet rs = null;
     PreparedStatement pst = null;
     creditos = new LinkedList();
     try {
         ManejadorBaseDatos mbd=ManejadorBaseDatos.getInstancia();
         mbd.conectar();
         con=mbd.getConexion();
        pst = con.prepareStatement(sql);
        rs = pst.executeQuery();
        while(rs.next()) {
           creditos.add(load(rs));
        }
    } finally {
        if (rs != null) {
           rs.close();
       }
        if (pst != null) {
          pst.close();
       }
}
}
  public static void setCredito(List aCredito) {
        creditos = aCredito;
    }
  public static List getCredito() {
        return creditos;
    }
   public static String ultimoCredito() throws Exception {
     ResultSet rs = null;
     PreparedStatement pst = null;
     String ultimoc="";
     DecimalFormat sd=new DecimalFormat("000");
     try {
         ManejadorBaseDatos mbd=ManejadorBaseDatos.getInstancia();
         mbd.conectar();
         con=mbd.getConexion();
        pst = con.prepareStatement("SELECT COUNT(id)+1 AS ultimo FROM creditos");
        rs = pst.executeQuery();
        while(rs.next()) {           
           String ultimo=rs.getString("ultimo");
           int nro=Integer.parseInt(ultimo);
           ultimoc="CR"+vistas.Fechacodigio()+sd.format(nro);
           
        }
    } finally {
        if (rs != null) {
           rs.close();
       }
        if (pst != null) {
          pst.close();
       }
    }
    return ultimoc;
}
}
